package com.example.tests;

import static com.example.fw.RandomizeHelper.getRandomNumber;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

import java.util.Iterator;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.example.utils.SortedListOf;

public class ContactModificationTests extends TestBase
{
	@Override
	@DataProvider
	public Iterator<Object[]> randomValidContactGenerator()
	{
		return super.randomValidContactGenerator();
	}

	@Test(dataProvider = "randomValidContactGenerator")
	public void modifySomeContact(ContactData contact)
	{
		SortedListOf<ContactData> oldList = app.getModel().getContacts();
		if (oldList.size() > 0)
		{
			int index = getRandomNumber(0, oldList.size() - 1);

			app.getContactHelper().modifyContact(contact, index);

			SortedListOf<ContactData> newList = app.getModel().getContacts();

			assertThat(newList, equalTo(oldList.without(index).withAdded(contact)));

			if (timeToCheck())
			{
				if ("yes".equals(app.getProperty("check.db")))
				{
					assertThat(newList, equalTo(app.getHibernateHelper().getContacts()));
				}
				if ("yes".equals(app.getProperty("check.ui")))
				{
					assertThat(newList, equalTo(app.getContactHelper().getUIContacts()));
				}
			}
		}
	}
}
